<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
/**
 * Created by PhpStorm.
 * LotteryGift: Administrator
 * Date: 2020/3/14
 * Time: 17:18
 */

namespace app\store\controller;
use app\core\InitController;

use think\facade\Session;
use think\facade\Cookie;
use think\facade\View;
use app\common\Common;
use app\store\validate\Login as LoginValidate;
use app\store\model\User as UserModel;
use app\store\model\UserToken;
class Qqlogin extends InitController
{
         // 绑定检查
    public function auth()
    {
        $post = $this->request->post();  
        // ajax回传数据处理
        $username = $this->request->post('name');
        $password = $this->request->post('pass');

        $openid = $this->request->post('openid');
       
        // 实例化验证器
        $validate = new LoginValidate();
        $admin = $validate->check($post);
 
        // 输出验证器错误信息
        if (true !== $admin) return $this->error($validate->getError());
        $auth = UserModel::auth($username);
        if (!$auth) return $this->error('用户名不存在！');
        if ($auth['status'] <> 1) {
            return $this->error('账号已异常，请联系管理员！');
        }

            // 效验码密码
       
        if ($auth->password !== md5($password . $auth->salt)) {
            // 记一次登录失败次数
            $auth->save(['loginfailure'=>$auth->loginfailure + 1]);
            return $this->error('密码错误！');
        }
         UserModel::where(['status'=>1, 'username'=>$username])->update(['openid'=>$openid]);
        //判断连续登录和最大连续登录
        if ($auth->login_time < Common::unixtime('day')) {
            $auth->successions = $auth->login_time < Common::unixtime('day', -1) ? 1 : $auth->successions + 1;
            $auth->maxsuccessions = max($auth->successions, $auth->maxsuccessions); // 更新登录时间
        $auth->save(['loginip' => $this->request->ip(), 'prev_time'=>$auth->login_time, 'login_time'=>time(), 'login_time'=>time()]);
            } elseif ($auth->login_time > Common::unixtime('day')) {// 更新登录时间
        $auth->save(['loginip' => $this->request->ip(), 'prev_time'=>$auth->login_time, 'login_time'=>time(), 'login_time'=>time()]);
         }
        // 设置token
        Session::set('userToken', $auth->token);
        Cookie::set('userToken', $auth->token, 7200);
        add_user_log('auth', 'QQ');
        return $this->success(true, 0, '绑定成功！', url('user/index'), 1);
    }
  
      // 处理qq登录
    public function login()
    {
     //应用APP ID
     $app_id = "101549464";
      //应用APP Key
     $app_secret = "06a102ab136ec68afa05624792e5061a";
      //应用填写的网站回调域
     $my_url = request()->domain()."/qqlogin/login";
     
      //Step1：获取Authorization Code
      $code=isset($_REQUEST["code"])?$_REQUEST["code"]:0;//存放Authorization Code
   
      if(empty($code)) {
        //state参数用于防止CSRF攻击，成功授权后回调时原样带回
        $state = md5(uniqid(rand(), TRUE));
        Session::set('state',$state);
        //拼接URL
        $dialog_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&state=".Session::get('state');
        return redirect($dialog_url)->send();
    }
       $stream_opts = [
         "ssl" => [
             "verify_peer"=>false,
             "verify_peer_name"=>false,
           ]
        ]; 

       //$response = file_get_contents("https://xxx.xxx.xxx",false, stream_context_create($stream_opts));
        //Step2：通过Authorization Code获取Access Token
       if($_REQUEST['state'] == Session::get('state') || 1) {
        //拼接URL
        $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"."client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&client_secret=".$app_secret."&code=".$code;
        //$response = file_get_contents($token_url);
         $response = file_get_contents($token_url,false, stream_context_create($stream_opts));
 
        //如果用户临时改变主意取消登录，返回true!==false,否则执行step3  
        if (strpos($response, "callback") !== false) {
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos -1);
            $msg = json_decode($response);
            if (isset($msg->error)) {
               
              return $msg->error.$msg->error_description;
            }
        }
  
        //Step3：使用Access Token来获取用户的OpenID
        $params = array();
        parse_str($response, $params);//把传回来的数据参数变量化
        $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];
        $str = file_get_contents($graph_url,false, stream_context_create($stream_opts));
        if (strpos($str, "callback") !== false) {
            $lpos = strpos($str, "(");
            $rpos = strrpos($str, ")");
            $str = substr($str, $lpos + 1, $rpos - $lpos -1);
        }
        $user = json_decode($str);//存放返回的数据 client_id ，openid
        if (isset($user->error)) {
           
            return $user->error.$user->error_description;
        }
  
        //Step4：使用openid和access_token获取用户信息
        $user_data_url = "https://graph.qq.com/user/get_user_info?access_token={$params['access_token']}&oauth_consumer_key={$app_id}&openid={$user->openid}&format=json";
      
        $user_data = json_decode(file_get_contents($user_data_url,false, stream_context_create($stream_opts)));//获取到的用户信息
        //以下为授权成功后的自定义操作
        if($user_data){
            $auth =UserModel::where(['openid'=>$user->openid])->where('status', 1)->find();
        if (isset($auth['openid'])==$user->openid){
           $list = UserToken::where(['user_id' => $auth->id])->findOrEmpty();
           //系统设置
             if($list->expire_time <= time()){
        return $this->success(true, 0, '请验证身份',url('user/authentication?email='.base64_encode($auth->email)));
              }
           if ($auth->login_time < Common::unixtime('day')) {
            $auth->successions = $auth->login_time < Common::unixtime('day', -1) ? 1 : $auth->successions + 1;
            $auth->maxsuccessions = max($auth->successions, $auth->maxsuccessions);
                  // 更新登录时间
        $auth->save(['loginip' => $this->request->ip(), 'prev_time'=>$auth->login_time, 'login_time'=>time(), 'login_num' => $auth['login_num'] + 1]);
        } elseif ($auth->login_time > Common::unixtime('day')) {
                  // 更新登录时间
        $auth->save(['loginip' => $this->request->ip(), 'prev_time'=>$auth->login_time, 'login_time'=>time(), 'login_num' => $auth['login_num'] + 1]);
        }
           // 保存状态
                $keep = [
                    'user_id' => $auth->id,
                    'username' => $auth->username,
                ];
            session::set('user_auth', $keep);
            // 设置token
            Session::set('userToken', $auth->token);

        add_user_log('login', 'QQ登录');
        //return $this->success('', 0, '验证成功，正在登录', url('user/index'));
        }else{
            
          View::assign('name',$user_data->nickname);
          View::assign('image',$user_data->figureurl_qq_2);
          View::assign('openid',$user->openid);
          return View::fetch('/user/bind');

        
     }

        }else{
       
            return $this->error('未知错误！');
        }
    }else{
         return $this->error('状态不匹配。你可能是CSRF的受害者');
    }
 
 }

}